package io.lvdaxian.handwriting.lock;

/**
 * 针对 字符串进行加锁
 *
 * @author lihh
 */
public class StringLock03 {
  public void save(String school) {
    System.out.println(school);
    try {
      Thread.sleep(500);
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }
  }
  
  public String saving(String school) {
    synchronized (school) {
      System.out.println(school + "学生交卷");
      save(school);
      System.out.println(school + "学生交卷完成");
      return "ok";
    }
  }
  
  public static void main(String[] args) throws InterruptedException {
    StringLock03 stringLock01 = new StringLock03();
    
    Thread t1 = new Thread(() -> stringLock01.saving("北大"));
    Thread t2 = new Thread(() -> stringLock01.saving("清华"));
    Thread t3 = new Thread(() -> stringLock01.saving("清华"));
    
    t1.start();
    t2.start();
    t3.start();
    
    t1.join();
    t2.join();
    t3.join();
  }
}
